package com.vsl.health;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    static TextView stepTextView;
    private ViewPager viewPager;
    private Toolbar toolbar;
    private ViewPagerAdapter adapter;
    private CollapsingToolbarLayout collapsingToolbar;

    /**
     * Handler对象用于更新当前步数,定时发送消息，调用方法查询数据用于显示
     * 主要接受子线程发送的数据, 并用此数据配合主线程更新UI
     * Handler运行在主线程中(UI线程中), 它与子线程可以通过Message对象来传递数据,
     * Handler就承担着接受子线程传过来的(子线程用sendMessage()方法传递Message对象，(里面包含数据)
     * 把这些消息放入主线程队列中，配合主线程进行更新UI。
     */
    static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            countDistance();     //调用距离方法，看一下走了多远
//            countStep();          //调用步数方法
//            countOthers();

            Log.d("步数", msg.getData().getString("total_step") + "");
            stepTextView.setText(msg.getData().getString("total_step") + "");

//            Intent intent = new Intent("com.vsl.health.broadcast.step");
//            intent.putExtra("total_step", total_step+"");
//            sendBroadcast(intent);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //启动记步服务，开始记步
        if (!StepCounterService.FLAG) {
            Intent startIntent = new Intent(this, StepCounterService.class);
            startService(startIntent);
        }

        init();
    }

    private void init() {
        stepTextView = (TextView) findViewById(R.id.stepTextview);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("运动监测");

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new TabFragment(), "运动");
        adapter.addFrag(new TabFragment(), "心率");
        adapter.addFrag(new TabFragment(), "统计");
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);
        collapsingToolbar.setTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

}
